/*
 *所有关于UpgradeFileManager类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*UpgradeFileManagerService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.RadarDevice.IRepositories;
using Newtonsoft.Json;
using System.Net;
using VOL.Core.Configuration;
using VOL.Core.DbContext;
using VOL.Entity.DomainModels.Devices.Dto;
using VOL.Core.ManageUser;

namespace VOL.RadarDevice.Services
{
    public partial class UpgradeFileManagerService
    {
        WebResponseContent webResponse = new WebResponseContent();
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IUpgradeFileManagerRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public UpgradeFileManagerService(
            IUpgradeFileManagerRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        /// <summary>
        /// 上传升级文件进行设备升级
        /// </summary>
        /// <param name="formData">参数</param>
        /// <param name="files">升级文件</param>
        /// <returns></returns>
        public WebResponseContent UploadFile(IFormCollection formData, IFormFile file)
        {
            var dir = AppSetting.UpgradeDir;
            string dicPath = dir + $"/Upload/{DateTime.Now.ToString("yyyMMdd")}/";
            if (!Directory.Exists(dicPath)) Directory.CreateDirectory(dicPath);
            var fileName = $"{DateTime.Now.ToString("HHmmssfff")}_{file.FileName}";
            dicPath = dicPath + fileName ;

            var filetype = formData["filetype"].ToString();
            var uType = 0;
            int.TryParse(filetype, out uType);

            using (var stream = new FileStream(dicPath, FileMode.Create))
            {
                file.CopyTo(stream);
            }
            if (!File.Exists(dicPath))
            {
                webResponse.Code = "-1";
                webResponse.Message = "升级文件获取失败，请重新上传！";
                return webResponse.Error(webResponse.Message);
            }
            var userId = UserContext.Current.UserId;
            var user = UserContext.Current.GetUserInfo(userId);

            UpgradeFileManager entity = new UpgradeFileManager()
            {
                CreateId = userId,
                createTime = DateTime.Now,
                Creator = user.UserTrueName,
                uType = uType,
                remarks = uType == 0 ? "雷达升级使用文件" : "主板升级使用文件",
                fileName=fileName,
                filePath=dicPath
            };
            _repository.Add(entity, true);

            webResponse.Code = "1";
            webResponse.Message = "升级文件上传成功";
            return webResponse.OK();
        }
    }
}
